home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8906 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  53 lines

  1. Path: info.uah.edu!oreo!gbacon
  2. From: gbacon@oreo (Greg Bacon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: hex to dec function?
  5. Date: 6 Mar 1996 23:56:04 GMT
  6. Organization: The University of Alabama in Huntsville
  7. Message-ID: <4hl8mk$1er@info.uah.edu>
  8. References: <4gdh1b$bg@mailhost.mwmicro.com>
  9. Reply-To: gbacon@CS.UAH.Edu
  10. NNTP-Posting-Host: oreo.aspire.cs.uah.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. aschlies@citynet.net wrote:
  14. : Hi There,
  15.  
  16. : Is there a function that converts HEX to Dec in ANSI C?
  17.  
  18. : Why re-invent the wheel?
  19.  
  20. : Thanks
  21. : Tony
  22.  
  23. Recite this mantra with me:
  24. "All numbers in a digital computer are stored in binary, regardless of
  25. program representation."
  26.  
  27. Assuming you have a string representing some hexadecimal quantity and
  28. you want to print its equivalent decimal value:
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33. /*
  34.  :
  35.  :
  36.  */
  37. {
  38.    char *strHexVal = "20";  /* 32 auf decimal */
  39.  
  40.    (void) printf("%d\n", strtol(strHexVal, (char **) NULL, 10));
  41.  
  42. However, there is no convenient specifier to the printf family that
  43. converts to an arbitrary base (but you can do all the useful ones,
  44. namely 8, 10, and 16).  If you want to do this, you'll have to
  45. code your own function to do so.
  46.  
  47. Hope this helps,
  48. Greg
  49. --
  50. Greg Bacon <gbacon@cs.uah.edu>
  51. University of Alabama in Huntsville
  52. CS Department Systems Support Team
  53.